package com.good.ui;

import com.good.dao.foodTableDao;
import com.good.po.foodtable;

import java.awt.*;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class mainUi extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private JTextField searchNameTextField;


	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					mainUi frame = new mainUi();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public mainUi() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 755, 478);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(139, 96, 562, 322);
		contentPane.add(scrollPane);
		
		table = new JTable();
		scrollPane.setViewportView(table);
		
		foodTableDao ftd = new foodTableDao();
		DefaultTableModel dtm = ftd.selectAllForTable(null);
		table.setModel(dtm);
		
		JButton btnNewButton = new JButton("增加");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {


			}
		});
		btnNewButton.setBounds(272, 56, 113, 27);
		contentPane.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("删除");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 这是删除
				
				int row = table.getSelectedRow();
				System.out.println("--------------row:" + row);
				if(row < 0){
					JOptionPane.showMessageDialog(null, "请选中一行删除!");
				}else{
					int i = JOptionPane.showConfirmDialog(null, "您确定要删除吗?");
					System.out.println("--------------i:" + i);
					if(i == 0){
						String id = (String)table.getValueAt(row, 0);
						foodTableDao ftd = new foodTableDao();
						System.out.println("--------------del id:" + id);
						ftd.delete(id);
						// 做刷新
						DefaultTableModel dtm = ftd.selectAllForTable(null);
						table.setModel(dtm);
					}
				}
			}
		});
		btnNewButton_1.setBounds(410, 56, 113, 27);
		contentPane.add(btnNewButton_1);
		
		JButton btnNewButton_2 = new JButton("修改");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 这是修改
				int row = table.getSelectedRow();
				System.out.println("--------------row:" + row);
				if(row < 0){
					JOptionPane.showMessageDialog(null, "请选中一行进行修改!");
		
				}else{
					String fid   = (String)table.getValueAt(row, 0);
					String fname = (String)table.getValueAt(row, 1);
					foodTableDao ftd = new foodTableDao();
					foodtable ft = new foodtable(fid, fname, "", "", "");
					ftd.update(ft);
				}
			}
		});
		btnNewButton_2.setBounds(552, 56, 113, 27);
		contentPane.add(btnNewButton_2);

		searchNameTextField = new JTextField();
		searchNameTextField.setBounds(148, 26, 254, 24);
		contentPane.add(searchNameTextField);
		searchNameTextField.setColumns(10);
		
		JButton btnNewButton_3 = new JButton("查询");
		btnNewButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String name = searchNameTextField.getText();
				System.out.println("--------------name:" + name);
				foodTableDao ftd = new foodTableDao();
				DefaultTableModel dtm = ftd.selectAllForTable(name);
				table.setModel(dtm);
			}
		});
		btnNewButton_3.setBounds(145, 56, 113, 27);
		contentPane.add(btnNewButton_3);
		
	}
}
